const express = require('express')
const router = express.Router()

const authorModel = require('../../models/authorModel')

router.post('/add', async(req,res)=>{
    try{
      await authorModel.create(req.body)
      res.json({
        code:200,
        msg:'创建成功',
        data:req.body
    })
    } catch(err){
        res.json({
            code:500,
            msg:'创建失败',
            err
        })
    }
})

router.post('/del', async(req,res)=>{
    try{
      await authorModel.deleteOne({_id:req.body.id})
      res.json({
        code:200,
        msg:'删除成功',
    })
    } catch(err){
        res.json({
            code:500,
            msg:'删除失败',
            err
        })
    }
})
router.get("/page", async (req, res) => {
    try {
      let list = await authorModel.find().select('-__v');
      list = list.map((item) => {
        const { _id, ...re } = item.toObject();
        return {
          ...re,
          id: _id,
        };
      });
      res.json({
        code: 200,
        msg: "成功",
        list
      });
    } catch (err) {
      res.json({
        code: 500,
        msg: "失败",
        err,
      });
    }
  });
module.exports = router